import { getsettlementMethod, getsettlementMethodName, getunitMethod } from '../../util/pagemethods'

export const postformat = (entry, isPrew) => {
  // try {
  let basicInfo = entry.basic_info
  let companyInfo = entry.companyInfo
  let bossInfo = entry.bossInfo
  
  const jobtopdetail = {
    id: basicInfo.id,
    collectionId: entry.collectionId,
    jobname: basicInfo.name,
    jobprice: basicInfo.salary,
    priceunit: getunitMethod(basicInfo.partUnitValue, basicInfo.settlementMethod, basicInfo.type),
    settlementMethod: getsettlementMethodName(basicInfo.settlementMethod),
    isFast: basicInfo.isFast,
    type: basicInfo.type,
    practiceLabel: basicInfo.practiceLabel,
    jobTags: basicInfo.category.split(','),
    viewpeople: entry.viewCount,
    zanpeople: entry.resume_count,
    sharepeople: entry.entry_count,
    baomingpeople: 0,
    education: geteducationvalue(basicInfo.education),
    ...companyInfo
  }
  let workarea = []
  if (basicInfo.workPlace && basicInfo.workPlace.length) {
    workarea = !isPrew ? dealWorkarea(basicInfo.workPlace) : basicInfo.workPlace
  }
  const jobbottomdetail = {
    workActive: 0,
    mapLocation: workarea[0]
      ? { latitude: workarea[0].latitude, longitude: workarea[0].longitude }
      : null,
    // worktime: basicInfo.workHours,
    workContent: basicInfo.workContent,
    workarea: workarea,
    welfare: basicInfo.welfare
  }
  const bossdetail = {
    mobile: bossInfo.mobile,
    user_name: bossInfo.userName,
    postion: bossInfo.position,
    user_avator: bossInfo.avator
  }

  return {
    jobtopdetail,
    jobbottomdetail,
    bossdetail,
    showEmpty: false
  }
  // } catch (e) {
  //
  // 	return {
  // 		showEmpty: true
  // 	}
  // }
}
// getPostDetail接口数据
export const postformatcommon = (entry, landmarkName, isCollect) => {
  // isCollect : 是否从岗位收藏里面进入详情页面
  // try {
  const { basicInfo, bossInfo, companyInfo } = entry
  const jobtopdetail = {
    id: entry.id,
    collectionId: entry.collectionId,
    jobname: basicInfo.name,
    jobprice: basicInfo.salary,
    priceunit: getunitMethod(basicInfo.unit, basicInfo.settlementMethod, basicInfo.type),
    settlementMethod: getsettlementMethodName(basicInfo.settlementMethod),
    isFast: basicInfo.isFast,
    type: basicInfo.type,
    practiceLabel: basicInfo.practiceLabel,
    jobTags: basicInfo.category.split(','),
    viewCount: entry.viewCount,
    resumeCount: entry.resumeCount,
    entryCount: entry.entryCount,
    collectionCount: entry.collectionCount,
    education: geteducationvalue(basicInfo.education),
    locations: entry.postWorkLocations.filter((e, i) => e.cityName == landmarkName).map(e => e.areaName),
    welfare: basicInfo.welfare,
    ...companyInfo
  }

  let workarea = []
  if (entry.postWorkLocations) {
    workarea = dealWorkarea(entry.postWorkLocations, landmarkName, isCollect)
  }
  const jobbottomdetail = {
    // worktime: basicInfo.workHours,
    workarea,
    workActive: 0,
    mapLocation: workarea[0]
      ? { latitude: workarea[0].latitude, longitude: workarea[0].longitude }
      : null,
    workContent: basicInfo.workContent,
    welfare: basicInfo.welfare
  }
  const bossdetail = {
    mobile: bossInfo.mobile,
    user_name: bossInfo.userName,
    postion: bossInfo.position,
    user_avator: bossInfo.avator,
    userIdB: bossInfo.userId,
    sex: bossInfo.sex
  }

  return {
    jobtopdetail,
    jobbottomdetail,
    bossdetail,
    showEmpty: false
  }
  // } catch (e) {
  //
  // 	return {
  // 		showEmpty: true
  // 	}
  // }
}
function geteducationvalue(value) {
  const res = [
    { value: 6, name: '学历不限' },
    { value: 1, name: '中专' },
    { value: 2, name: '专科' },
    { value: 3, name: '本科' },
    { value: 4, name: '硕士' },
    { value: 5, name: '博士' }
  ].find(e => e.value == value)
  return res ? res.name : ''
}

function dealWorkarea(workarea, landmarkName, isCollect) {
  // 将当前定位城市的地址排序靠前 非当前城市去掉
  let workareaNew = []
  workarea.map(item => {
    if (item.cityName.replace('市', '') == landmarkName) {
      if (isCollect) {
        workareaNew.unshift(item)
      } else {
        workareaNew.push(item)
      }
    } else {
      if (isCollect) workareaNew.push(item)
    }
  })
  return workareaNew
}
